<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="原始ID" prop="jwid">
        <el-input
          v-model="queryParams.jwid"
          placeholder="请输入公众号原始ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleScanAdd"
          v-hasPermi="['wxmp:wxconfig:add']"
        >扫码授权
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wxmp:wxconfig:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wxmp:wxconfig:export']"
        >导出
        </el-button>
      </el-col>

      <el-tooltip :showSearch.sync="showSearch" @queryTable="getList"></el-tooltip>
    </el-row>

    <el-table v-loading="loading" :data="jwidList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公众号原始ID"  align="center" prop="jwid" class="el-table-custom"/>
      <el-table-column label="公众号名称" align="center" prop="name"/>
      <el-table-column label="应用类型" align="center" prop="applicationType"/>
      <el-table-column label="微信二维码图片" align="center" prop="qrcodeimg"/>
      <el-table-column label="微信号" align="center" prop="weixinNumber"/>
      <el-table-column label="微信AppId" align="center" prop="weixinAppid"/>
      <el-table-column label="微信AppSecret" align="center" prop="weixinAppsecret"/>
      <el-table-column label="公众号类型" align="center" prop="accountType"/>
      <el-table-column label="是否认证" align="center" prop="authStatus"/>
      <el-table-column label="Access_Token" align="center" prop="accessToken"/>
      <el-table-column label="token获取的时间" align="center" prop="tokenGettime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tokenGettime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="api凭证" align="center" prop="apiticket"/>
      <el-table-column label="apiticket获取时间" align="center" prop="apiticketGettime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.apiticketGettime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="jsapiticket" align="center" prop="jsapiticket"/>
      <el-table-column label="jsapiticket获取时间" align="center" prop="jsapiticketGettime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jsapiticketGettime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型：1手动授权，2扫码授权" align="center" prop="authType"/>
      <el-table-column label="功能的开通状况：1代表已开通" align="center" prop="businessInfo"/>
      <el-table-column label="公众号授权给开发者的权限集" align="center" prop="funcInfo"/>
      <el-table-column label="授权方昵称" align="center" prop="nickName"/>
      <el-table-column label="授权方头像" align="center" prop="headimgurl"/>
      <el-table-column label="授权信息" align="center" prop="authorizationInfo"/>
      <el-table-column label="刷新token" align="center" prop="authorizerRefreshToken"/>
      <el-table-column label="令牌" align="center" prop="token"/>
      <el-table-column label="授权状态" align="center" prop="authorizationStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wxmp:wxconfig:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {listJwid, getJwid, delJwid, addJwid, updateJwid, exportJwid} from "@/api/wxmp/wxconfig";
  import { Base64 } from 'js-base64';
  import {getWxAuthInfo} from "@/api/wxmp/wxauth";


  export default {
    name: "wxconfig",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 系统公众号表格数据
        jwidList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          jwid: null,
          name: null,
          applicationType: null,
          qrcodeimg: null,
          weixinNumber: null,
          weixinAppid: null,
          weixinAppsecret: null,
          accountType: null,
          authStatus: null,
          accessToken: null,
          tokenGettime: null,
          apiticket: null,
          apiticketGettime: null,
          jsapiticket: null,
          jsapiticketGettime: null,
          authType: null,
          businessInfo: null,
          funcInfo: null,
          nickName: null,
          headimgurl: null,
          authorizationInfo: null,
          authorizerRefreshToken: null,
          token: null,
          authorizationStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          jwid: [
            {required: true, message: "公众号不能为空", trigger: "blur"}
          ],
          name: [
            {required: true, message: "名称不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询系统公众号列表 */
      getList() {
        this.loading = true;
        listJwid(this.queryParams).then(response => {
          this.jwidList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          jwid: null,
          name: null,
          applicationType: null,
          qrcodeimg: null,
          createBy: null,
          createTime: null,
          weixinNumber: null,
          weixinAppid: null,
          weixinAppsecret: null,
          accountType: null,
          authStatus: "0",
          accessToken: null,
          tokenGettime: null,
          apiticket: null,
          apiticketGettime: null,
          jsapiticket: null,
          jsapiticketGettime: null,
          authType: null,
          businessInfo: null,
          funcInfo: null,
          nickName: null,
          headimgurl: null,
          authorizationInfo: null,
          authorizerRefreshToken: null,
          token: null,
          authorizationStatus: "0"
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加系统公众号";
      },
      /** 扫码授权操作*/
      handleScanAdd() {
        console.log("getWxAuthInfo")
        getWxAuthInfo({
          base_api: process.env.VUE_APP_BASE_API
        })
          .then(response => {
            //跳转到授权页面
            const href = Base64.encode(response.data.url);//将地址URL base64加密方便传输
            console.log("handleScanAdd url: ")
            console.log(response.data.url)

            console.log(href)

            //跳转到新的页面
            window.open("/wxmp/wxauth?url=" + href, '_blank')
          });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getJwid(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改系统公众号";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateJwid(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJwid(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除系统公众号编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delJwid(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有系统公众号数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportJwid(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style >
</style>
